//
//  DialDetailInformationViewController.m
//  ChengXinBao
//
//  Created by RongZhou on 17/2/14.
//  Copyright © 2017年 RongZhou. All rights reserved.
//

#import "DialDetailInformationViewController.h"
#import "LLKCollectionManage.h"
#import "FlipTableView.h"
#import "SegmentTapView.h"
#import "DialPersonInforViewController.h"
#import "DialRecoredViewController.h"
#import <Masonry.h>
@interface DialDetailInformationViewController ()<SegmentTapViewDelegate,FlipTableViewDelegate>

@property(nonatomic,strong)NSMutableArray <CallModel *>*userInformationArray;
@property (nonatomic, strong)SegmentTapView *segment;
@property (nonatomic, strong)FlipTableView *flipView;
@end

@implementation DialDetailInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"联系人详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getUSerDialRecordInformation];
    [self createUIAboutDialRecordContacterInformation];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
-(void)createBackBtn{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 6, 67, 23);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
    backImg.frame = CGRectMake(-10, 0, 22, 22);
    [backBtn addSubview:backImg];
    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
    backText.text = @"返回";
    backText.font = [UIFont systemFontOfSize:17];
    [backText setBackgroundColor:[UIColor clearColor]];
    [backText setTextColor:[UIColor whiteColor]];
    [backBtn addSubview:backText];
    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];

}
-(void)leftBarButtonItemPressed:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUIAboutDialRecordContacterInformation{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 200)];
    view.backgroundColor = [UIColor colorWithRed:(45 / 255.0f) green:(145 / 255.0f) blue:(253 / 255.0f) alpha:1];
    [self.view addSubview:view];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"contacterIcon"];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(40);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    if ([self.userName isEqual:self.phoneNumber]) {
        nameLabel.text = @"";
    }else{
      nameLabel.text = self.userName;
    }
    nameLabel.textColor = [UIColor whiteColor];
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(ScreenWidth/2);
        make.centerX.equalTo(view.mas_centerX);
    }];
    self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30) withDataArray:[NSArray arrayWithObjects:@"详细信息",@"通话记录", nil] withFont:15];
    self.segment.delegate = self;
    self.segment.lineColor = [UIColor blueColor];
    self.segment.textNomalColor = [UIColor lightGrayColor];
    self.segment.textSelectedColor = [UIColor blueColor];
    [self.view addSubview:self.segment];
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.height.mas_equalTo(30);
    }];
    DialPersonInforViewController *personVc = [[DialPersonInforViewController alloc] init];
    personVc.phoneStr = self.phoneNumber;
    personVc.userStr = self.userName;
    personVc.phoneNumberArea = self.phoneNumberArea;
    DialRecoredViewController *recordVc = [[DialRecoredViewController alloc] init];
    recordVc.phoneStr = self.phoneNumber;
    recordVc.userInforArray = self.userInformationArray;
    self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) withArray:[NSArray arrayWithObjects:personVc,recordVc, nil]];
    self.flipView.delegate = self;
    [self.view addSubview:self.flipView];
    [self.flipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segment.mas_bottom).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];

}

-(void)getUSerDialRecordInformation{
    dispatch_queue_t globaleQueue = dispatch_get_global_queue(0, 0);
    dispatch_async(globaleQueue, ^{
        NSMutableArray *array = [[LLKCollectionManage shareManager] selectAll];
        NSLog(@"%@",[NSThread currentThread]);
        for (CallModel *model in array) {
            if ([model.userPhonenumber isEqual:self.phoneNumber]) {
                [self.userInformationArray addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self createUIAboutDialRecordContacterInformation];
        });
    });
}
#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.flipView selectIndex:index];
    
}
-(void)scrollChangeToIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.segment selectIndex:index];
}

-(NSMutableArray<CallModel *> *)userInformationArray{
    if (_userInformationArray == nil) {
        _userInformationArray = [[NSMutableArray alloc] init];
    }
    return _userInformationArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
